package com.gitee.feizns.quickstart.log.spring.boot.autoconfigure;

import com.gitee.feizns.quickstart.log.LogUserIdSupplier;
import com.gitee.feizns.quickstart.log.spring.boot.autoconfigure.db.MybatisFlexSaveToDbLogListener;
import com.gitee.feizns.quickstart.log.spring.boot.autoconfigure.db.mybatis.flex.pojo.QkLog;
import com.mybatisflex.core.activerecord.Model;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.Entity;

/**
 * qk log db 自动配置
 * @author feizns
 * @since 2025/1/7
 */
@ConditionalOnClass({Entity.class, Model.class})
@ConditionalOnWebApplication
@ConditionalOnProperty(value = "quickstart.log.save-to-db-enabled", havingValue = "true", matchIfMissing = true)
@EntityScan("com.gitee.feizns.quickstart.log.spring.boot.autoconfigure.db.mybatis.flex.pojo")
@MapperScan("com.gitee.feizns.quickstart.log.spring.boot.autoconfigure.db.mybatis.flex.mapper")
@Configuration
@ConditionalOnMissingBean(MybatisFlexSaveToDbLogListener.class)
public class MybatisFlexQkLogSaveToDbAutoConfigure {

    /**
     * 保存到数据库日志侦听器
     * @return {@link MybatisFlexSaveToDbLogListener}
     */
    @Bean
    public MybatisFlexSaveToDbLogListener<QkLog> mybatisFlexSaveToDbLogListener() {
        return new MybatisFlexSaveToDbLogListener<>(QkLog.class);
    }

}
